#ifndef MESSAGE_HANDLER_H
#define MESSAGE_HANDLER_H

#include <iostream>
#include "MessageQueue.h"
#include "SockStream.h"

class MessageHandler
{
protected:
	MessageHandler() {}
	virtual ~MessageHandler() {}

public:
	// Calling interface to the outside world.
	void HandleMessage( MessageQueue& );
	void HandleMessage( std::istream& );

	// Message composing functions.
	template<typename content_type>
	static std::ostream& PutMessage( std::ostream&, const content_type& );
	// A specialization that accounts for a sockstream's flushing needs.
	template<typename content_type>
	static std::ostream& PutMessage( sockstream& s, const content_type& c )
	{ PutMessage( static_cast<std::ostream&>( s ), c ); s.flush(); return s; }

protected:
	// Callback interface for inheritants to hook in. The return value indicates
    // whether the content was read (and removed) from the stream.
    // Because template functions cannot be virtual we need to explicitly
    // enumerate all of them.
    // Note that the name of a handler should be Handle<ClassName> to
    // allow the CONSIDER macro to work.
	virtual bool HandleSignal(	std::istream& )		{ return false; }
	virtual bool HandleCommand(	std::istream& )		{ return false; }
	virtual bool HandleStatus(  std::istream& )		{ return false; }
private:
	template<typename content_type> struct Header;
	void CheckForError( std::istream&, const char* );
};

class Signal;
template<> struct MessageHandler::Header<Signal>
{ enum{descSupp = 0x0000}; };

class Command;
template<> struct MessageHandler::Header<Command>
{ enum{descSupp = 0x0100}; };

class Status;
template<> struct MessageHandler::Header<Status>
{ enum{descSupp = 0x0200}; };

#endif // MESSAGE_HANDLER_H